package com.vegas.netspider.dto;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.vegas.netspider.betdefine.BallSocreBet;
import com.vegas.netspider.betdefine.CapotBet;
import com.vegas.netspider.betdefine.ConcedeBet;
import com.vegas.netspider.betdefine.HalfAndFullMatchBet;
import com.vegas.netspider.betdefine.OddAndEvenBet;
import com.vegas.netspider.betdefine.OtherBet;
import com.vegas.netspider.betdefine.OverAndUnderBet;
import com.vegas.netspider.betdefine.ScoreRangeBet;
import com.vegas.netspider.betdefine.panko.Banquancang;
import com.vegas.netspider.betdefine.panko.Bodan;
import com.vegas.netspider.betdefine.panko.DanJie;
import com.vegas.netspider.betdefine.panko.Danshi;
import com.vegas.netspider.betdefine.panko.Gunqiu;
import com.vegas.netspider.betdefine.panko.Quanbu;
import com.vegas.netspider.betdefine.panko.Rangqiushu;
import com.vegas.netspider.betdefine.panko.Shangbanbodan;
import com.vegas.netspider.betdefine.panko.Shangbancang;
import com.vegas.netspider.betdefine.panko.Zongheguoguan;
import com.vegas.netspider.betdefine.time.FirstHalf;
import com.vegas.netspider.betdefine.time.FirstJie;
import com.vegas.netspider.betdefine.time.FourthJie;
import com.vegas.netspider.betdefine.time.FullMatch;
import com.vegas.netspider.betdefine.time.SecondHalf;
import com.vegas.netspider.betdefine.time.SecondJie;
import com.vegas.netspider.betdefine.time.ThirdJie;

public class SportMatchUtil {

	public static boolean isSameType(SportMatch o1,SportMatch o2){
		if((o1 instanceof Banquancang) && !(o2 instanceof Banquancang)){
			return false;
		}
		if((o1 instanceof Bodan) && !(o2 instanceof Bodan)){
			return false;
		}
		if((o1 instanceof Danshi) &&!(o2 instanceof Danshi)){
			return false;
		}
		if((o1 instanceof Gunqiu) && !(o2 instanceof Gunqiu)){
			return false;
		}
		if((o1 instanceof Rangqiushu) && !(o2 instanceof Rangqiushu)){
			return false;
		}
		if((o1 instanceof Shangbanbodan) && !(o2 instanceof Shangbanbodan)){
			return false;
		}
		if((o1 instanceof Shangbancang) && !(o2 instanceof Shangbancang)){
			return false;
		}
		if((o1 instanceof Zongheguoguan) && !(o2 instanceof Zongheguoguan)){
			return false;
		}
		if((o1 instanceof DanJie) && !(o2 instanceof DanJie)){
			return false;
		}
		if((o1 instanceof Quanbu) && !(o2 instanceof Quanbu)){
			return false;
		}
		
		if((o1 instanceof FirstHalf) && !(o1 instanceof FirstHalf)){
			return false;
		}
		if((o1 instanceof FirstJie) && !(o2 instanceof FirstJie)){
			return false;
		}
		if((o2 instanceof FourthJie) && !(o2 instanceof FourthJie)){
			return false;
		}
		if((o1 instanceof FullMatch) && !(o2 instanceof FullMatch)){
			return false;
		}
		if((o1 instanceof SecondHalf) && !(o2 instanceof SecondHalf)){
			return false;
		}
		if((o1 instanceof SecondJie) && !(o2 instanceof SecondJie)){
			return false;
		}
		if((o1 instanceof ThirdJie) && !(o2 instanceof ThirdJie)){
			return false;
		}
		
		return true;
	}
	
	private static Pattern playDatePattern = Pattern.compile("(\\d{1,2}-\\d{1,2})");
	private static Pattern playTimePattern = Pattern.compile("(\\d{2}:\\d{2})[ap]");
	private static SimpleDateFormat playTimeSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	public static long parsePlayTime(String datetime){
		try {
			Matcher matcher = playDatePattern.matcher(datetime);
			matcher.find();
			String date = matcher.group(1);
			matcher = playTimePattern.matcher(datetime);
			matcher.find();
			String time = matcher.group(1);
			Date _date = playTimeSdf.parse(Calendar.getInstance().get(Calendar.YEAR)+"-"+date+" "+time);
			long _dateLong = _date.getTime();
			
			int i = datetime.indexOf("p");
			if(i>12 && i<16){
				if(time.indexOf("12")==-1||time.indexOf("12")>time.indexOf(":")){				
					_dateLong += 12*60*60*1000;
				}
			}
			
			long now = System.currentTimeMillis();
			if(_dateLong<now){
				Calendar nowCal = Calendar.getInstance();
				nowCal.setTimeInMillis(_dateLong);
				nowCal.add(Calendar.YEAR, 1);
				_dateLong = nowCal.getTimeInMillis();
			}
			
			return _dateLong;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	
	public static String parseRunningTime(String s){
		return s.substring(1,s.length()-1);
	}
	
	public static Integer parseMatchId(String res){
		String id = res.substring(1,res.length()-1);
		if(id.trim().equals("")){
			return null;
		}
		return Integer.valueOf(id);
	}
	
	public static boolean willPlay(String res){
		if(res.indexOf("Running")>0){
			return true;
		}
		return false;
	}
	
	public static String parseLeagueName(String res){
		return res.substring(1,res.length()-1);
	}
	
	public static Integer parseHomeTeamId(String res){
		return Integer.valueOf(res.substring(1,res.length()-1));
	}
	
	public static Integer parseGuestTeamId(String res){
		return Integer.valueOf(res.substring(1,res.length()-1));
	}
	
	public static String parseHomeTeamName(String res){
		return res.substring(1,res.length()-1);
	}
	
	public static String parseGuestTeamName(String res){
		return res.substring(1,res.length()-1);
	}
	
	public static ConcedeBet parseConcedeBet(String team,String num,String oddH,String oddG,Integer matchId){
		team = team.substring(1,team.length()-1);
		if(team.trim().equals("")){
			return null;
		}
		num = num.substring(1,num.length()-1);
		if(num.trim().equals("")){
			return null;
		}
		oddH = oddH.substring(1,oddH.length()-1);
		if(oddH.trim().equals("")){
			return null;
		}
		oddG = oddG.substring(1,oddG.length()-1);
		if(oddG.trim().equals("")){
			return null;
		}
		
		ConcedeBet cb = new ConcedeBet(team, num, Double.valueOf(oddH), Double.valueOf(oddG), matchId);
		return cb;
	}
	
	
	public static OverAndUnderBet parseOverAndUnderBet(String overNum,String overOdds,String underNum,String underOdds,Integer matchid){
		overNum = overNum.substring(1,overNum.length()-1);
		if(overNum.trim().equals("")){
			return null;
		}
		
		overOdds = overOdds.substring(1,overOdds.length()-1);
		if(overOdds.trim().equals("")){
			return null;
		}
		
		underNum = underNum.substring(1,underNum.length()-1);
		if(underNum.trim().equals("")){
			return null;
		}
		
		underOdds = underOdds.substring(1,underOdds.length()-1);
		if(underOdds.trim().equals("")){
			return null;
		}
		
		OverAndUnderBet oaub = new OverAndUnderBet(overNum, Double.valueOf(overOdds), underNum, Double.valueOf(underOdds), matchid);
		
		return oaub;
	}
	
	public static CapotBet parseCapotBet(String htOdds,String gtOdds,String drOdds,Integer matchId){
		htOdds = htOdds.substring(1,htOdds.length()-1);
		if(htOdds.trim().equals("")){
			return null;
		}
		
		gtOdds = gtOdds.substring(1,gtOdds.length()-1);
		if(gtOdds.trim().equals("")){
			return null;
		}
		
		drOdds = drOdds.substring(1,drOdds.length()-1);
		if(drOdds.trim().equals("")){
			return null;
		}
		
		CapotBet cb = new CapotBet(Double.valueOf(htOdds),Double.valueOf(gtOdds),Double.valueOf(drOdds),matchId);
		return cb;
	}
	
	
	public static OddAndEvenBet parseOddAndEvenBet(String oddOdds,String evenOdds,Integer matchId){
		oddOdds = oddOdds.substring(1,oddOdds.length()-1);
		if(oddOdds.trim().equals("")){
			return null;
		}
		
		evenOdds = evenOdds.substring(1,evenOdds.length()-1);
		if(evenOdds.trim().equals("")){
			return null;
		}
		
		OddAndEvenBet oab = new OddAndEvenBet(Double.valueOf(oddOdds), Double.valueOf(evenOdds), matchId);
		return oab;
	}
	
	public static int parseMoreNum(String s){
		s = s.substring(1,s.length()-1);
		if(s.trim().equals("")){
			return 0;
		}
		
		return Integer.valueOf(s);
	}
	
	public static int parseNumber(String s){
		s = s.substring(1,s.length()-1);
		if(s.trim().equals("")){
			return 0;
		}
		
		return Integer.valueOf(s);
	}
	
	public static String parseString(String s){
		return s.substring(1,s.length()-1);
	}
	
	public static BallSocreBet parseBallSocreBet(int hts,int gts,String odds,Integer matchId){
		/*hts = parseString(hts);
		if(hts.trim().equals("")){
			return null;
		}
		
		gts = parseString(gts);
		if(gts.trim().equals("")){
			return null;
		}*/
		
		odds = parseString(odds);
		if(odds.trim().equals("")){
			return null;
		}
		return new BallSocreBet(hts,gts,Double.valueOf(odds),matchId);
		
	}
	
	public static ScoreRangeBet parseScoreRangeBet(int f,int e,String odds,Integer matchId){
		odds = parseString(odds);
		if(odds.trim().equals("")){
			return null;
		}
		
		return new ScoreRangeBet(f,e,Double.valueOf(odds),matchId);
	}
	
	
	public static HalfAndFullMatchBet parseHalfAndFullMatchBet(String h,String f,String odds,Integer matchId){
		odds = parseString(odds);
		if(odds.trim().equals("")){
			return null;
		}
		
		return new HalfAndFullMatchBet(h,f,Double.valueOf(odds),matchId);
	}
	
	
	public static OtherBet parseOtherBet(String type,String hodds,String godds,String nodds,Integer matchId){
		hodds = parseString(hodds);
		if(hodds.trim().equals("")){
			return null;
		}
		
		godds = parseString(godds);
		if(godds.trim().equals("")){
			return null;
		}
		
		nodds = parseString(nodds);
		if(nodds.trim().equals("")){
			return null;
		}
		
		OtherBet ob = new OtherBet(type, Double.valueOf(hodds), Double.valueOf(godds), Double.valueOf(nodds), matchId);
		return ob;
		
	}
}
